package com.interesting.examsystem.common.config;

import com.interesting.examsystem.common.config.handlerInterceptor.LoginHandlerInterceptor;
import com.interesting.examsystem.common.config.handlerInterceptor.WebRequestHandlerInterceptor;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ClassName: InterceptorConfig
 * @Author: ZhengXinyi
 * @Description: 拦截器配置
 * @Date: 2021/7/24 19:42
 * @Version: 1.0
 */

@Configuration
@AllArgsConstructor
public class InterceptorConfig implements WebMvcConfigurer {

    //登录拦处理
    private LoginHandlerInterceptor loginHandlerInterceptor;

    //Web访问拦截处理
    private WebRequestHandlerInterceptor webRequestHandlerInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginHandlerInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/v1/sys/user/login/**")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
        registry.addWebRequestInterceptor(webRequestHandlerInterceptor)
                .addPathPatterns("/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
                .allowCredentials(true).maxAge(3600);
    }

}